package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Company;
import com.itheima.domain.store.Question;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/question")
public class QusetionServlet  extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if ("list".equals(operation)) {
            this.list(request, response);
        } else if ("toAdd".equals(operation)) {
            this.toAdd(request, response);
        } else if ("save".equals(operation)) {
            try {
                this.save(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("toEdit".equals(operation)) {
            this.toEdit(request, response);
        } else if ("edit".equals(operation)) {
            try {
                this.edit(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("delete".equals(operation)) {
            this.delete(request, response);
        }else if ("toTestUpload".equals(operation)) {
            this.toTestUpload(request, response);
        }else if ("testUpload".equals(operation)) {
            try {
                this.testUpload(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void toTestUpload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher("/WEB-INF/pages/store/question/testFileUpload.jsp").forward(request, response);

    }
    private void testUpload(HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (ServletFileUpload.isMultipartContent(request)){
            DiskFileItemFactory factory=new DiskFileItemFactory();
            ServletFileUpload fileupload=new ServletFileUpload(factory);
            List<FileItem> fileItems = fileupload.parseRequest(request);

        /* Question question =BeanUtil.fillBean(request,Question.class);
         questionService.save(question);*/
            for (FileItem item : fileItems){
                if(!item.isFormField()){
                    item.write(new File(this.getServletContext().getRealPath("upload"),item.getName()));
                }
            }
        }
        /* request.getRequestDispatcher("/WEB-INF/pages/store/question/list.jsp").forward(request, response);*/
    }
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // QuestionService questionService=new QuestionServiceImpl();
        int page = 1;
        int size = 5;
        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
            page = Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = questionService.findAll(page, size);
        request.setAttribute("page", all);
        request.getRequestDispatcher("/WEB-INF/pages/store/question/list.jsp").forward(request, response);

    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Catalog> catalogList = catalogService.findAll();
        List<Company> companyList= companyService.findAll();
        request.setAttribute("catalogList",catalogList);
        request.setAttribute("companyList",companyList);
        request.getRequestDispatcher("/WEB-INF/pages/store/question/add.jsp").forward(request, response);

    }

    private void save(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //1.确认该操作是否支持文件上传操作，enctype="multipart/form-data"
        if(ServletFileUpload.isMultipartContent(request)){
            //2.创建磁盘工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //3.Servlet文件上传核心对象
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            //4.从request中读取数据
            List<FileItem> fileItems = fileUpload.parseRequest(request);

            //进行图片有无的判断
            boolean flag = false;
             for (FileItem item : fileItems){
                 if(StringUtils.isNotBlank(item.getName())){
                  flag=true;
                  break;
                 }
             }
            // --处理form表单提交过来的普通数据
            //将数据获取到，封装成一个对象
            Question question = BeanUtil.fillBean(fileItems,Question.class);
            //调用业务层接口save
            String picture = questionService.save(question,flag);

            // --处理form表单提交过来的文件数据
            for(FileItem item : fileItems){
                //5.当前表单是否是文件表单
                if(!item.isFormField()){
                    //6.从临时存储文件的地方将内容写入到指定位置
                    item.write(new File(this.getServletContext().getRealPath("upload"),picture));
                }
            }
        }
        //跳转回到页面list
        response.sendRedirect(request.getContextPath()+"/store/question?operation=list");
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        //  QuestionService questionService=new QuestionServiceImpl();
        Question question = questionService.findById(id);
        request.setAttribute("question", question);

        List<Catalog> catalogList = catalogService.findAll();
        List<Company> companyList= companyService.findAll();
        request.setAttribute("catalogList",catalogList);
        request.setAttribute("companyList",companyList);
        request.getRequestDispatcher("/WEB-INF/pages/store/question/update.jsp").forward(request, response);
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.确认该操作是否支持文件上传操作，enctype="multipart/form-data"
        if(ServletFileUpload.isMultipartContent(request)){
            //2.创建磁盘工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //3.Servlet文件上传核心对象
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            //4.从request中读取数据
            List<FileItem> fileItems = fileUpload.parseRequest(request);

            //进行图片有无的判断
            boolean flag = false;
            for (FileItem item : fileItems){
                if(StringUtils.isNotBlank(item.getName())){
                    flag=true;
                    break;
                }
            }

            // --处理form表单提交过来的普通数据
            //将数据获取到，封装成一个对象
            Question question = BeanUtil.fillBean(fileItems,Question.class);
            //调用业务层接口save
            questionService.update(question,flag);

            // --处理form表单提交过来的文件数据
            for(FileItem item : fileItems){
                //5.当前表单是否是文件表单
                if(!item.isFormField()){
                    //6.从临时存储文件的地方将内容写入到指定位置
                    item.write(new File(this.getServletContext().getRealPath("upload"),question.getId()));
                }
            }
        }
        response.sendRedirect(request.getContextPath() + "/store/question?operation=list");
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Question question = BeanUtil.fillBean(request, Question.class);
        //  QuestionService questionService=new QuestionServiceImpl();
        questionService.delete(question);
        response.sendRedirect(request.getContextPath() + "/store/question?operation=list");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}